import { Menu, Icon } from 'antd';
import * as React from 'react';
import { Link } from 'react-router';
const { Item: MenuItem, ItemGroup: MenuItemGroup } = Menu;

const menuMap = {
  SYS_ADMIN: {
    text: '系统管理员',
    menus: [
      { icon: 'team', text: '企业管理', href: '/console/company-manage' },
      { icon: 'hdd', text: '机器管理', href: '/console/machine-manage' },
      { icon: 'user', text: '用户管理', href: '/console/user-manage' }
    ]
  },
  COMPANY_ADMIN: {
    text: '企业管理员',
    menus: [
      { icon: 'team', text: '我维护的企业', href: '/console/company-admin' }
    ]
  },
  COMMON: {
    text: '用户',
    menus: [
      { icon: 'home', text: '控制台首页', href: '/console' },
      { icon: 'code', text: '我的开发组', href: '/console/my-dev-teams' },
      { icon: 'share-alt', text: '我的项目', href: '/console/my-runnable-groups' },
      { icon: 'cloud-o', text: '服务治理', href: '/console/dubbo-profiler' }
    ]
  }
};

const Sidebar = ({ loginUser }) => {
  const menuGroups = [menuMap.COMMON];
  if (loginUser.hasRole('COMPANY_ADMIN')) {
    menuGroups.push(menuMap.COMPANY_ADMIN);
  }
  if (loginUser.hasRole('SYS_ADMIN')) {
    menuGroups.push(menuMap.SYS_ADMIN);
  }

  const p = window._profile_;

  const logoClass = p ? `logo logo-${p}` : 'logo';

  return (
    <div className='console-sidebar'>
      <div className='logo-box'>
        <Link to='/'>
          <div className={ logoClass }></div>
        </Link>
      </div>
      <Menu theme='dark'
        mode='inline'
        className='sidebar-menu'
      >
        {menuGroups.map(({ text, menus }) => {
          return (
            <MenuItemGroup title={text} key={text}>
              {menus.map(menu => {
                return (
                  <MenuItem key={menu.text}>
                    <Link to={menu.href}>
                      <Icon type={menu.icon}/>
                      <span>{menu.text}</span>
                    </Link>
                  </MenuItem>
                );
              })}
            </MenuItemGroup>
          );
        })}
      </Menu>
    </div>
  );
};

export default Sidebar;
